﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Quanly_dethi.Class;
namespace Quanly_dethi
{
    public partial class Them_ND : Form
    {
        private csB_Admin ad;
        private csB_Quyen q;
        int k = 0;
        private string tenmay = bienketnoi.tenmay;
        private string taikhoan = bienketnoi.taikhoan;
        private string matkhau = bienketnoi.matkhau;
        private string tencsdl = bienketnoi.tencsdl;
        public Them_ND()
        {
            
            ad = new csB_Admin(tenmay, taikhoan, matkhau, tencsdl);
            q = new csB_Quyen(tenmay, taikhoan, matkhau, tencsdl);
            InitializeComponent();
            load();
            loadcb_quyen();
        }

        private void Bt_Them_Click(object sender, EventArgs e)
        {
            csE_Admin ad_moi = new csE_Admin();
            ad_moi.Tennguoidung = txt_TenND.Text;
            ad_moi.Matkhau = txt_matkhau.Text;
            ad_moi.Maquyen = int.Parse(Cb_Quyen.SelectedValue.ToString());
            ad_moi.Xoa = 0;
            ad.Insert(ad_moi);
            load();

        }
        private void load()
        {
            DataTable viewdata = new DataTable("viewdata");

            DataColumn themcot = viewdata.Columns.Add("MAND", typeof(int));
            themcot.AllowDBNull = true;
            viewdata.Columns.Add("Tennguoidung", typeof(string));
            viewdata.Columns.Add("Mk", typeof(string));
            viewdata.Columns.Add("Quyen", typeof(string));

            string tennguoidung = "", quyen = "", matkhau="";
            for (int i = 0; i < ad.select_all().Rows.Count; i++)
            {
                int ma = int.Parse(ad.select_all().Rows[i][0].ToString());
                tennguoidung = ad.select_all().Rows[i][1].ToString();
                matkhau = ad.select_all().Rows[i][2].ToString();
                quyen = ad.select_all().Rows[i][3].ToString();
                viewdata.Rows.Add(ma,tennguoidung,matkhau,quyen);
            }

            drv_danhsachnd.DataSource = viewdata;
        }
        private void loadcb_quyen()
        {
            Cb_Quyen.DataSource = q.SelectArr();
            Cb_Quyen.DisplayMember = "Name";
            Cb_Quyen.ValueMember = "Value";
        }

        private void drv_danhsachnd_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int i = e.RowIndex;
                k = int.Parse(drv_danhsachnd[0, i].Value.ToString());
                txt_TenND.Text = drv_danhsachnd[1, i].Value.ToString();
                txt_matkhau.Text = drv_danhsachnd[2, i].Value.ToString();
                Cb_Quyen.Text = drv_danhsachnd[3, i].Value.ToString();
                if (drv_danhsachnd[1, i].Value.ToString() == "")
                {
                    MessageBox.Show("Bạn click vào ô không có dữ liệu!!");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
                
        }

        private void bt_sua_Click(object sender, EventArgs e)
        {
            csE_Admin ad_sua = new csE_Admin();
            ad_sua.Manguoidung = k;
            ad_sua.Tennguoidung = txt_TenND.Text;
            ad_sua.Matkhau = txt_matkhau.Text;
            ad_sua.Maquyen = int.Parse(Cb_Quyen.SelectedValue.ToString());
            ad_sua.Xoa = 0;
            ad.Update(ad_sua);
            load();
        }
    }
}
